#pragma once
#include "Protocol.hpp"
#include <iostream>

class Caculate
{

public:
    Caculate() = default;
    ~Caculate() = default;
    Response Excute(const Request &request)
    {
        int result = 0;
        switch (request.Oper())
        {
        case '+':
            result = request.X() + request.Y();
            break;
        case '-':
            result = request.X() - request.Y();
            break;
        case '*':
            result = request.X() * request.Y();
            break;
        case '/':
            if (request.Y() == 0)
                return Response(0, -1);
            result = request.X() / request.Y();
            break;
        default:
            return Response(0, -1);
        }
        return Response(result, 0);
    }
}cal;

std::string Hander(std::string &in)
{
        LOG(LogLevel::DEBUG) << "hander :" ;

    std::string request_str;
    std::string result;
    while (Decode(in, &request_str))
    {
        std::string resp_str;
        if (request_str.empty())
            break;

        //1. 反序列化
        Request req;
        if (!req.Deserialize(request_str))
        {
            break;
        }
        LOG(LogLevel::DEBUG)<<"反序列化成功";
        std::cout << "#############" << std::endl;
        req.Print();
        std::cout << "#############" << std::endl;
        //2. 业务
        Response resp = cal.Excute(req);
        //3 .序列化
        resp.Serialize(resp_str);
        //4.添加报头
        Encode(resp_str);
        //5.汇总总报头
        result += resp_str;
    }
    return result;
}